using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using Steamworks;
using UnityEngine;

public class SteamSaveable : MonoBehaviour
{
	public static void SaveObject(object dataIn, string fileName)
	{
		Save(ToByteArray(dataIn), fileName);
	}

	public static void Save(byte[] dataIn, string fileName)
	{
		string text = (SteamManager.Initialized ? SteamUser.GetSteamID().ToString() : "default");
		string path = Application.persistentDataPath + "/" + text + "/" + fileName.Replace("/", "_") + ".sav";
		if (!Directory.Exists(Application.persistentDataPath + "/" + text))
		{
			Directory.CreateDirectory(Application.persistentDataPath + "/" + text);
		}
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		FileStream fileStream = new FileStream(path, FileMode.Create);
		binaryFormatter.Serialize(fileStream, dataIn);
		fileStream.Close();
	}

	public static object LoadObject(string fileName)
	{
		return ToObjectFromArray(Load(fileName));
	}

	public static byte[] Load(string fileName)
	{
		string text = (SteamManager.Initialized ? SteamUser.GetSteamID().ToString() : "default");
		string path = Application.persistentDataPath + "/" + text + "/" + fileName.Replace("/", "_") + ".sav";
		if (File.Exists(path))
		{
			BinaryFormatter binaryFormatter = new BinaryFormatter();
			FileStream fileStream = new FileStream(path, FileMode.Open);
			byte[] result = binaryFormatter.Deserialize(fileStream) as byte[];
			fileStream.Close();
			return result;
		}
		return null;
	}

	public static bool HasSave(string fileName)
	{
		string text = (SteamManager.Initialized ? SteamUser.GetSteamID().ToString() : "default");
		if (File.Exists(Application.persistentDataPath + "/" + text + "/" + fileName.Replace("/", "_") + ".sav"))
		{
			return true;
		}
		return false;
	}

	public static void DeleteSaveKey(string fileName)
	{
		string text = (SteamManager.Initialized ? SteamUser.GetSteamID().ToString() : "default");
		string path = Application.persistentDataPath + "/" + text + "/" + fileName.Replace("/", "_") + ".sav";
		if (File.Exists(path))
		{
			File.Delete(path);
		}
	}

	private static byte[] ToByteArray(object source)
	{
		if (source == null)
		{
			return null;
		}
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		using MemoryStream memoryStream = new MemoryStream();
		binaryFormatter.Serialize(memoryStream, source);
		return memoryStream.ToArray();
	}

	private static object ToObjectFromArray(byte[] data)
	{
		if (data == null)
		{
			return null;
		}
		if (data.Length == 0)
		{
			return null;
		}
		BinaryFormatter binaryFormatter = new BinaryFormatter();
		using MemoryStream serializationStream = new MemoryStream(data);
		return binaryFormatter.Deserialize(serializationStream);
	}

	public static int GetInt(string fileName, int defaultValue = 0)
	{
		try
		{
			if (!HasSave(fileName))
			{
				return defaultValue;
			}
			object obj = LoadObject(fileName);
			if (obj == null)
			{
				return defaultValue;
			}
			return (int)obj;
		}
		catch (Exception)
		{
			return defaultValue;
		}
	}

	public static bool GetBool(string fileName, bool defaultValue = false)
	{
		if (!HasKey(fileName))
		{
			return defaultValue;
		}
		return GetInt(fileName) == 1;
	}

	public static string GetString(string fileName, string defaultValue = "")
	{
		try
		{
			if (!HasSave(fileName))
			{
				return defaultValue;
			}
			object obj = LoadObject(fileName);
			if (obj == null)
			{
				return defaultValue;
			}
			return obj.ToString();
		}
		catch (Exception)
		{
			return defaultValue;
		}
	}

	public static float GetFloat(string fileName, float defaultValue = 0f)
	{
		try
		{
			if (!HasSave(fileName))
			{
				return defaultValue;
			}
			object obj = LoadObject(fileName);
			if (obj == null)
			{
				return defaultValue;
			}
			return (float)obj;
		}
		catch (Exception)
		{
			return defaultValue;
		}
	}

	public static void SetInt(string fileName, int value)
	{
		SaveObject(value, fileName);
	}

	public static void SetString(string fileName, string value)
	{
		if (value != null)
		{
			SaveObject(value, fileName);
		}
	}

	public static void SetBool(string fileName, bool value)
	{
		SetInt(fileName, value ? 1 : 0);
	}

	public static void SetFloat(string fileName, float value)
	{
		SaveObject(value, fileName);
	}

	public static void Save()
	{
	}

	public static bool HasKey(string key)
	{
		return HasSave(key);
	}
}
